<?php

class places{
	function getPlaceList(){
		$placeList = array();
		qdbconn();
		
		$query = "SELECT * FROM places";
		$result = mysql_query($query);
		
		$i = 0;
		while ($row = mysql_fetch_row($result)) {
			$placeList[$i]['pla_id']	= $row[0];
			$placeList[$i]['pla_name']= $row[1];
			$placeList[$i]['pla_address']= $row[2];
			$placeList[$i]['pla_type']= $row[3];
			$placeList[$i]['pla_status']= $row[4];
			
			
			$i++;
		}
		return $placeList;
	}
	
	function inactivateAPlace($pla_id){
		qdbconn();		
		$query = "UPDATE places SET pla_status = 0 where pla_id = $pla_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function activateAPlace($pla_id){
		qdbconn();		
		$query = "UPDATE places SET pla_status = 1 where pla_id = $pla_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function editAPlace($data){
		qdbconn();
		$pla_id 		= $data['pla_id'];
		$pla_name 		= $data['pla_name'];
		$pla_address 	= $data['pla_address'];
		$pla_type 		= $data['pla_type'];
		
		
		$query = "SELECT count(1) FROM places
					WHERE pla_name = '$pla_name' 
						and pla_address = '$pla_address' and pla_type='$pla_type'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) //pla_name khong bi trung
			{
				$query = "UPDATE places SET pla_name='$pla_name', pla_address='$pla_address',
								pla_type = '$pla_type' WHERE pla_id = $pla_id";
				
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}			
	}
	
	function insertAPlace($data){
		qdbconn();
		$pla_name	 = $data['pla_name'];
		$pla_address = $data['pla_address'];
		$pla_type	 = $data['pla_type'];
		
		
		$query = "SELECT count(1) FROM places
					WHERE pla_name = '$pla_name' 
					and pla_address = '$pla_address' and pla_type='$pla_type'" ;
		
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) //pla_name khong bi trung
			{
				$query = "INSERT INTO places (pla_name,pla_address,pla_type) 
							values ('$pla_name,$pla_address,$pla_type')";
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}		
	}
	
	
	
	function getPlacesInfo($pla_id){
		qdbconn();		
		$query = "SELECT * FROM places
					WHERE pla_id = '$pla_id'";
		$result = mysql_query($query);
		$current_data = array();
		if ($row = mysql_fetch_row($result)) {
			$current_data['pla_id'] 		= $row[0];
			$current_data['pla_name'] 		= $row[1];
			$current_data['pla_address'] 	= $row[2];
			$current_data['pla_type'] 		= $row[3];
			$current_data['pla_status'] 	= $row[4];
			
		}
		return $current_data;
	}

}

?>